﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

namespace ArgusPCLib.Mathematics
{
	public class BigNumberFormatInfo : IFormatProvider
	{
		NumberFormatInfo numberFormatInfo;
		int b;
		//string digitSeparator;
		//string baseSeparator;
		int basePattern;

		public NumberFormatInfo NumberFormatInfo
		{
			get { return this.numberFormatInfo; }
			set
			{
				if (value == null)
					throw new ArgumentNullException();
				this.numberFormatInfo = value;
			}
		}

		public int Base
		{
			get { return this.b;}
			set
			{
				if (value < 2)
					throw new ArgumentOutOfRangeException();
				this.b = value;
			}
		}

		public string DigitSeparator { get; set; }
		public string BaseSeparator { get; set; }

		/// <summary>
		/// 0: base_number
		/// 1: number_base
		/// Where '_' is the <see cref="BaseSeparator"/>.
		/// </summary>
		public int BasePattern
		{
			get { return this.basePattern; }
			set
			{
				if (value < 0 || value > 1)
					throw new ArgumentOutOfRangeException();
				this.basePattern = value;
			}
		}

		public BigNumberFormatInfo()
		{
			this.NumberFormatInfo = new NumberFormatInfo();
			this.Base = 10;
			this.DigitSeparator = "|";
			this.BaseSeparator = "_";
			this.BasePattern = 0;
		}

		public static BigNumberFormatInfo CurrentInfo
		{
			get
			{
				BigNumberFormatInfo nfi = new BigNumberFormatInfo();
				nfi.NumberFormatInfo = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
				return nfi;
			}
		}

		public static BigNumberFormatInfo InvariantIfno { get { return new BigNumberFormatInfo(); } }

		public object GetFormat(Type formatType)
		{
			if (formatType == typeof(BigNumberFormatInfo))
				return this;
			if (formatType == typeof(NumberFormatInfo))
				return this.NumberFormatInfo;
			return null;
		}

		public static BigNumberFormatInfo GetInstance(IFormatProvider formatProvider)
		{
			if (formatProvider == null)
				return BigNumberFormatInfo.CurrentInfo;
			BigNumberFormatInfo RetVal = formatProvider.GetFormat(typeof(BigNumberFormatInfo)) as BigNumberFormatInfo;
			if (RetVal == null)
				return BigNumberFormatInfo.CurrentInfo;
			return RetVal;
		}
	}
}
